//
// Created by wujehy on 2020/5/22.
//

#include "ConsultCipther.h"
#include "SecurityCommon.h"
//#include "GeeJoanMD5.h"
using namespace GeeJoan::Common;

int CiptherBase::EnCipther(
        std::string &inputString,
        std::string &key,
        std::string &vi,
        CpitherCallbackFunction callback,
        void *userdata)
{
    if (key.size() != 32)
    {
        return COMMON_ERROR_CODE_INVAL;
    }

    if (vi.size() !=16)
    {
        return COMMON_ERROR_CODE_INVAL;
    }
    std::string temp_output;
    int result =  common_encrypt(temp_output , CIPHER_AES_CTR_NOPADDING, (uint8_t*)key.c_str() , key.size() ,(uint8_t*)  vi.c_str() ,vi.size() , (uint8_t*)inputString.c_str() , inputString.size() );

    if  (result < 0 )
    {
        //  失败
        return COMMON_ERROR_CODE_FAIL;
    } else
    {
        // 成功
        if(callback)
        {
            callback((void*)temp_output.c_str() , temp_output.length() , userdata);
        }
        return COMMON_ERROR_CODE_SUCCESS;

    }
}

int CiptherBase::DeCipther(
        std::string &inputString,
        std::string &key,
        std::string &vi,
        CpitherCallbackFunction callback,
        void *userdata)
{
    if (key.size() != 32)
    {
        return COMMON_ERROR_CODE_INVAL;
    }

    if (vi.size() !=16)
    {
        return COMMON_ERROR_CODE_INVAL;
    }

    std::string temp_output;
    int result =  common_decrypt(temp_output , CIPHER_AES_CTR_NOPADDING, (uint8_t*)key.c_str() , key.size() ,(uint8_t*)  vi.c_str() ,vi.size() , (uint8_t*)inputString.c_str() , inputString.size() );

    if  (result < 0 )
    {
        //  失败
        return COMMON_ERROR_CODE_FAIL;
    } else
    {
        // 成功
        if(callback)
        {
            callback((void*)temp_output.c_str() , temp_output.length() , userdata);
        }
        return COMMON_ERROR_CODE_SUCCESS;

    }
}

ConsultCipther::ConsultCipther(const KeyPairData &mKeypair) : m_keypair(mKeypair)
{

}

int ConsultCipther::ConsultEnCipther(std::string &inString, std::string &otherpublicKey, std::string &output)
{
    std::string shared_key;
    if (m_keypair.Calculation(shared_key, otherpublicKey) < 0)
    {
        return COMMON_ERROR_CODE_INVAL;
    }
    //TODO　自定义　的16 个字符的 初始向量
    std::string vi = "0123456789ABCDEF";
    std::string temp;
    int result = this->EnCipther(inString, shared_key, vi, [](void *data, int data_len, void *userdata)
    {
        std::string *user_str = (std::string*)userdata;

        (*user_str) = std::string((char*)data , data_len);

    }, &temp);


    if (result < 0 )
    {
        // 失败
    } else
    {
        output = temp;
    }
    return result;
}

int ConsultCipther::ConsultDeCipther(std::string &inString, std::string &otherpublicKey, std::string &output)
{
    std::string shared_key;
    if (m_keypair.Calculation(shared_key, otherpublicKey) < 0)
    {
        return COMMON_ERROR_CODE_INVAL;
    }
    //TODO　自定义　的16 个字符的 初始向量
    std::string vi = "0123456789ABCDEF";
    std::string temp;
    int result = this->DeCipther(inString, shared_key, vi, [](void *data, int data_len, void *userdata)
    {
        std::string *user_str = (std::string *) userdata;

        (*user_str) = std::string((char *) data, data_len);

    }, &temp);


    if (result < 0 )
    {
        // 失败
    } else
    {
        output = temp;
    }
    return result;
}

